//
//  HMEmoticonViewModel.swift
//  01-表情
//
//  Created by mac on 15/10/25.
//  Copyright © 2015年 itheima. All rights reserved.
//

import Foundation

// MARK: -表情视图模型
class HMEmoticonManager {
    
    static let sharedManager = HMEmoticonManager()
    /// 懒加载数据包
    lazy var packages = [HMEmoticonPackages]()
    private init() {
        // 0.添加最近分组
        packages.append(HMEmoticonPackages(dict: ["group_name_cn":"最近A"]))
        // 1. 加载emoticons.plist文，如果文件不存在，path为nil
        let path = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")!
        // 加载字典
        let dict = NSDictionary(contentsOfFile: path) as! [String: AnyObject]
        // 从字典中获得id数组
        let array = (dict["packages"] as! NSArray).valueForKey("id")
        for id in array as! [String] {
            //print(id)
            loadInfoPlist(id)
        }
        
        print(packages)
    }
    private func loadInfoPlist(id: String) {
        // 获取路径 
        let path = NSBundle.mainBundle().pathForResource("info.plist", ofType: nil, inDirectory: "Emoticons.bundle/\(id)")!
        // 加载字典
        let dict = NSDictionary(contentsOfFile: path) as! [String: AnyObject]
        // 字典转模型，并且追加到数组中
        packages.append(HMEmoticonPackages(dict: dict))
    }
}